﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using Rhiannon.Extensions;

namespace Rhiannon.Windows.Presentation.Behaviors
{
	public class TreeViewSelectedItemBehaviour : Behavior<TreeView>
	{
		#region SelectedItem Property

		public object SelectedItem
		{
			get { return GetValue(SelectedItemProperty); }
			set { SetValue(SelectedItemProperty, value); }
		}

		public static readonly DependencyProperty SelectedItemProperty =
			DependencyProperty.Register("SelectedItem", typeof(object), typeof(TreeViewSelectedItemBehaviour), new UIPropertyMetadata(null, OnSelectedItemChanged));

		private static void OnSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
		{
			TreeViewSelectedItemBehaviour behaviour = (TreeViewSelectedItemBehaviour)sender;
			TreeView treeView = behaviour.AssociatedObject;
			treeView.SelectItem(e.NewValue);
		}

		#endregion

		protected override void OnAttached()
		{
			base.OnAttached();
			AssociatedObject.SelectedItemChanged += OnTreeViewSelectedItemChanged;
		}

		protected override void OnDetaching()
		{
			base.OnDetaching();
			if (AssociatedObject != null)
			{
				AssociatedObject.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
			}
		}

		private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
		{
			SelectedItem = e.NewValue;
		}
	}
}